package com.xr.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xr.constant.MessageConstant;
import com.xr.exception.CustomException;
import com.xr.pojo.Menu;
import com.xr.query.QueryPageBean;
import com.xr.result.PageResult;
import com.xr.result.Result;
import com.xr.service.MemuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
public class MenuController {

    //    遠程訂閲服務 注解注入
    @Reference
    private MemuService memuService;

    //查询所有+条件查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        System.out.println("value=>" + queryPageBean);
        PageResult page = memuService.findPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, page);
    }

    //查询除所有父级菜单
    @RequestMapping("/findByParent")
    public Result queryAllParentList() {
        try {
            List<Menu> menuList = memuService.queryAllParentList();
            return new Result(true, "查询父级菜单成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询父级菜单失败");
        }
    }

    //添加
    @RequestMapping("/insert")
    public Result add(Integer IsId, @RequestBody Menu menu) {
        try {
            System.out.println("join add==>" + IsId + " " + menu);
            if (IsId.equals(1)) {
                menu.setLevel(1);
                memuService.add(menu);
            } else {
                menu.setLevel(2);
                memuService.add(menu);
            }
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    //根据id查询
    @RequestMapping("/queryById")
    public Result queryById(Integer id) {
        try {
            Map map = new HashMap();
            Menu menu = memuService.queryById(id);
            map.put("menu", menu);
            return new Result(true, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    //修改
    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu) {
        try {
            memuService.update(menu);
            return new Result(true, "修改菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改菜单失败");
        }
    }

    //根据父id查询出菜单名
    @RequestMapping("/findByParentId")
    public Result findByParentId(Integer id) {
        try {
            Menu byParentId = memuService.findByParentId(id);
            System.out.println("findByParentIdVlue:" + byParentId);
            return new Result(true, "SUCCESS", byParentId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "FAIL");
        }

    }

    //删除
    @RequestMapping("/deleteById")
    public Result delete(Integer id) throws CustomException {
        memuService.delete(id);
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }

    @RequestMapping("/demo")
    public Result demo(Integer id) {

        try {
            memuService.delete(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }
}
